package controllers

import (
	"bufio"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"os"
)

func (ctrl *Default) Caiji(c *gin.Context) {
	str, err := Fetch("http://b2b.huangye88.com/jiangsu/")
	fmt.Println(str)
	if err == nil {
		fmt.Println(str)
	}
}

//根据网页链接获取到网页内容
func Fetch(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
	}

	bodyReader := bufio.NewReader(resp.Body)

	return ioutil.ReadAll(bodyReader)
}

func WriteFile(name, content string) {
	content += "\n"
	f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	defer f.Close()
	if err != nil {
		// 打开文件失败处理

	} else {
		// 查找文件末尾的偏移量
		n, _ := f.Seek(0, 2)
		// 从末尾的偏移量开始写入内容
		_, err = f.WriteAt([]byte(content), n)
	}
}
